package com.frkj.yuan.base;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import com.bumptech.glide.Glide;
import com.frkj.yuan.R;
import com.frkj.yuan.main.BaseApplication;

import butterknife.ButterKnife;
/**
 * Created by ${wangjiang} on 2017/9/1.
 * 所有activity 的基类，并封装了简单的动画效果
  * @author Administrator
 */
public abstract class BaseActivity extends AppCompatActivity {
    public Context mContext;
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        //当一个activty创建后就将其加入到list集合中
        ((BaseApplication) getApplication()).addActivity(this);
        setContentView(getLayoutId());
        if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        ButterKnife.bind(this);
        mContext = this;
        initLayoutView();
    }
    /**
     * 获取布局文件
     */
    public abstract int getLayoutId();
    /**
     * 初始化view
     */
    public abstract void initLayoutView();
    /**
     * 通过类名启动Activity带动画
     * @param pClass
     */
    public void openActivityWitchAnimation(Class<?> pClass) {
        openActivityWitchAnimation(pClass, null);
    }
    /**
     * 通过类名启动Activity，并且含有Bundle数据,带动画
     * @param pClass
     * @param pBundle
     */
    public void openActivityWitchAnimation(Class<?> pClass, Bundle pBundle) {
        Intent intent = new Intent(this, pClass);
        if (pBundle != null) {
            intent.putExtras(pBundle);
        }
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
        startActivity(intent);
        //跳转动画
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR){
            overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
        }
    }
    /**
     * 通过类名启动Activity 不带动画
     * @param pClass
     */
    public void openActivity(Class<?> pClass) {
        openActivity(pClass, null);
    }

    /**
     * 通过类名启动Activity，
     * 并且含有Bundle数据不包含动画
     * @param pClass
     * @param pBundle
     */
    public void openActivity(Class<?> pClass, Bundle pBundle) {
        Intent intent = new Intent(this, pClass);
        if (pBundle != null) {
            intent.putExtras(pBundle);
        }
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
        startActivity(intent);
    }
    /**
     * 带动画的退出
     */
    public void finishWitchAnimation() {
        finish();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) {
            overridePendingTransition(R.anim.slide_left_in, R.anim.slide_right_out);
        }
    }
    /**
     * 托付OnKeyDown事件
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finishWitchAnimation();
        }
        return super.onKeyDown(keyCode, event);
    }
    /**
     * 返回按钮的实现
     *
     * @param view 传入的返回View
     */
    public void BackClick(View view) {
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finishWitchAnimation();
            }
        });
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //当被销毁后，将其移除
        BaseApplication.removeActivity(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && !this.isDestroyed()) {
            Glide.with(this).pauseRequests();
        }
    }
    /**
     * @param activity
     *
     */
    public static void hideKeyBoard(Activity activity) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        //Find the currently focused view, so we can grab the correct window token from it.
        View view = activity.getCurrentFocus();
        //If no view currently has focus, create a new one, just so we can grab a window token from it
        if (view == null) {
            view = new View(activity);
        }
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);

    }

}
